package org.wmq.javafxmapcollectioncs;

import org.wmq.javafxmapcollectioncs.mapper.impl.MapEntityMapperImplByHttpAndMysql;
import org.wmq.javafxmapcollectioncs.model.Entity.MapEntity;
import org.wmq.javafxmapcollectioncs.model.common.MyList;
import org.wmq.javafxmapcollectioncs.service.CommentService;
import org.wmq.javafxmapcollectioncs.service.MapEntityService;
import org.wmq.javafxmapcollectioncs.service.impl.CommentServiceImpl;
import org.wmq.javafxmapcollectioncs.service.impl.MapEntityServiceImpl;
import com.alibaba.fastjson.JSON;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;
import java.util.List;

public class God {
    private God(){
        // 调用服务器的getAll方法获取所有数据
        String url = "http://localhost:8080/mapEntity/getAll";
        HttpGet httpGet = new HttpGet(url);
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
                // 获取状态码
                System.out.println(response.getVersion()); // HTTP/1.1
                System.out.println(response.getCode()); // 200
                System.out.println(response.getReasonPhrase()); // OK
                HttpEntity entity = response.getEntity();
                // 获取响应信息
                String responseString = EntityUtils.toString(entity);
                // 假设这里有一个方法将字符串转换为MapEntity对象列表
                List<MapEntity> mapEntities = JSON.parseArray(responseString, MapEntity.class);
                for (MapEntity mapEntity : mapEntities) {
                    mapEntityList.add(mapEntity);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final God god = new God();
    public static final God getGod(){
        return god;
    }

    private MyList<MapEntity> mapEntityList = new MyList<>();
    private MapEntityService mapEntityService = new MapEntityServiceImpl();
    private CommentService commentService = new CommentServiceImpl();

    public MyList<MapEntity> getMapEntityList(){
        return this.mapEntityList;
    }

    public MapEntityService getMapEntityService(){
        return this.mapEntityService;
    }

    public CommentService getCommentService(){
        return this.commentService;
    }
}